<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/9/11
 * Time: 14:43
 */

namespace app\admin\controller;

use app\service\OrderService;
use app\service\ExpressService;

/**
 * 销售统计.
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月22日10:57:35
 */
class SaleOrderData extends Common
{
    /**
     * 构造方法.
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月25日07:38:52
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

        $this->IsLogin();
    }

    /**
     * [Index 订单统计列表].
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:58:48
     */
    public function Index()
    {

        if ('ajax' != input('get.action')) {
            $this->assign('id_arr',input('get.id_arr'));
            return $this->fetch();
        }
        else {
            // 参数
            $params =input();


            // 条件
            $where = OrderService::OrderListWhere($params['param']);
            $where[]=['mo.pay_status','=',1];
            if(isset($params['id_arr']) && $params['id_arr']>0)
            {
                $where[] = ['mo.id', 'in', explode(',',$params['id_arr'])];
            }
           // dump($where);exit;
            // $where[]=['mod.order_status','>',1];

            $data_params = [
                'where' => $where,
                'page' => true,
                'number' => 10,
                'group'=>'mo.id',
                'orderby'=>'member_order.create_time',
                'field'=>'mo.order_number,mo.id mo_id,mo.create_time,mo.total_price,mo.pay_type ,mo.pay_price'
            ];
            $re=OrderService::OrderList($data_params);
            //dump($re);exit;
            //$total= OrderService::DataTotal('member_orderdetail',$where);

            $total= OrderService::DataTotal('mo.id',$where);
            $total_arr = OrderService::ToSendDataTotal(['mo.is_delete'=>2,'mo.pay_status'=>1]);

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re['data'],'totalnum'=>$total,'totalprice'=>$total_arr["tosendprice_total"],];
        }
    }

    /**
     * 订单状态页面
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月22日15:11:06
     */
    public function DataDetail(){
        // 参数
        $params = input();
        // 数据

        //下单人 手机号 下单时间 收货信息（姓名电话地址） 订单信息（名称 单位 数量 金额） 订单金额 满减金额 实际支付金额 支付方式 订单状态
        // 获取列表
        $data_params = [
            'where' => ['mo.id' => intval($params['mo_id']),'mod.is_delete'=>2],
            'm' => 0,
            'n' => 1,
            'page' => false,
            'group'=>'mo.id',
            'orderby'=>'member_order.create_time',
            'field'=>'mo.id mo_id,m.name,m.phone,mo.create_time,mo.address,mo.province,mo.city,mo.area,mo.receive_name,mo.receive_phone,mo.pay_price,mo.total_price,mo.cheap_price,mo.pay_type',
            //'mo.order_number,mo.id mo_id,mo.receive_name,mo.receive_phone,mo.create_time,mo.pay_price,mo.order_status,mo.province,mo.city,mo.area,mo.address'
        ];
        $ret=OrderService::OrderList($data_params);
        $data = empty($ret['data'][0]) ? [] : $ret['data'][0];
        // dump($data);exit;
        $this->assign('res', $data);

        return $this->fetch();
    }

    /**
     * 修改订单状态
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月22日15:11:06
     */
    public function EditStatus(){
        $params = input();
        $data_params = [
            'where' => ['mod.id' => intval($params['mod_id'])],
            'm' => 0,
            'n' => 1,
            'page' => false,
            'group'=>'mod.id',
            'orderby'=>'member_order.create_time',
            'field'=>'mod.id mod_id,mod.order_status,mod.orderdetail_status,mod.express_code,mod.logistic_id',
            //'mo.order_number,mo.id mo_id,mo.receive_name,mo.receive_phone,mo.create_time,mo.pay_price,mo.order_status,mo.province,mo.city,mo.area,mo.address'
        ];
        $ret=OrderService::OrderList($data_params);
        $data = empty($ret['data'][0]) ? [] : $ret['data'][0];

        $this->assign('res', $data);
        //物流列表
        $data_params  = [
            'where' => [['id','>',0]],
            'page' => false,
            'field' => '*',
            'table'=>'express',
        ];
        $re= ExpressService::DataList($data_params);
        $this->assign('express',$re);

        return $this->fetch();
    }
    /**
     * 修改订单状态
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月22日15:11:06
     */
    public function SaveStatus(){
        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }
        $params = $this->data_post;
        //dump($params);exit;
        switch ($params['data_status']){
            case 1:
                $update = ['orderdetail_status' => 1];
                break;
            case 2:
                $update = ['orderdetail_status'=>2];
                break;
            case 3:
                $update = ['order_status'=> 3,'orderdetail_status'=>2];
                break;
            case 4:
                $update = ['order_status'=>4,'orderdetail_status'=>2];
                break;
        }
        $update['logistic_id']=$params['logistic_id'];
        $update['express_code']=$params['express_code'];

        $data1['where']=['id'=>$params['mod_id']];
        $data1['update']=$update;
        $data1['table']='member_orderdetail';
        $data1['msg']='修改订单详情失败';
        $data1['errorCode']='4003';
        OrderService::SaveOrder($data1);
        if($update['orderdetail_status']==1)
        {
            OrderService::SaveSupPrice(['id'=>$params['mod_id']]);
        }
        return DataReturn('保存成功', 0);
    }
    /**
     * 删除订单
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月22日15:11:06
     */
    public function DelData(){
        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }
        $params = $this->data_post;

        $data1['where']=['id'=>$params['mod_id']];
        $data1['update']=['is_delete'=>1];
        $data1['table']='member_orderdetail';
        $data1['msg']='修改订单详情失败';
        $data1['errorCode']='4003';
        OrderService::SaveOrder($data1);
        return DataReturn('保存成功', 0);
    }
}